//  PULSAR, THE GAME
//  ================================
//  COPYRIGHT(c) 2013 BY KAMIL MRZYGŁÓD
//  CODENOVA.PL

define(['./Core'], function (Core) {
    var Stage = function(stageName, ws) {
        var windowDimensions = Core.GetWindowDimensions();

        this.Height = windowDimensions.Height;
        this.Id = Core.GenerateUUID();
        this.Name = stageName;
        this.Width = windowDimensions.Width;
        this.WS = ws;
        this.Viewports = [];
    };

    Stage.prototype = {
        Create: function(stageName, ws) {
            return new Stage(stageName, ws);
        },

        Initialize: function() {
            // Make sure we are connected to WS
            if(typeof this.WS === 'undefined' || this.WS.ReadyState != 1) {
                window.setTimeout(this.Initialize.bind(this), 100);
            } else {
                // Initialize UI
                this.Ui.Initialize();

                // Initialize all viewports
                this.Viewports['planet'].Initialize();

                // Loop stage
                this.Loop();
            }
        },

        Loop: function() {
            var self = this;

            // Create global loop for stage
            window.requestAnimationFrame(function() {
                // Draw Ui elements
                self.Ui.Add();

                // Draw Viewports
                self.Viewports['planet'].Add();

                //self.WS.Connection.send('aaa');

                // Loop
                self.Loop();
            }, null);
        },

        RegisterUi: function(ui) {
            Object.defineProperty(this, 'Ui', {
                value: ui(this),
                writable: true,
                enumerable: true,
                configurable: true
            });
        },

        RegisterViewport: function(viewport, viewportName) {
            this.Viewports[viewportName] = viewport(this);
        }
    };

    return Stage.prototype.Create;
});